package com.daocao.hua.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.adapter.YouhuiSelectAdapter;
import com.daocao.hua.api.callback.YouhuiSelectContract;
import com.daocao.hua.api.requestInfo.YouhuiSelectRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.event.YouhuiSelectEvent;
import com.daocao.hua.model.BonusModel;
import com.daocao.hua.util.OnLoadeMoreListener;
import com.daocao.hua.util.UI;
import com.daocao.hua.view.CircularProgress;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/8/21.
 */

public class YouhuiSelectActivity extends BaseActivity implements YouhuiSelectContract.View, SwipeRefreshLayout.OnRefreshListener, YouhuiSelectAdapter.OnShareClickListener {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.nothing)
    TextView nothing;
    @BindView(R.id.progressBar)
    CircularProgress loading;
    private YouhuiSelectAdapter adapter;
    private YouhuiSelectRequest api;
    private Unbinder unbinder;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            api.getYouhui(page, 10,1);
        }
    };
    private String bonus_id;
    private String name;
    private String money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_youhui_select);
        unbinder = ButterKnife.bind(this);
        baseInitView(this, "YouhuiSelectActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        api = YouhuiSelectRequest.create();
        api.attachView(this);
        setBackIntent();
        setTitleText("优惠券");
        adapter = new YouhuiSelectAdapter(this);
        adapter.setShareClickListener(this);
        list.setAdapter(adapter);
        list.addOnScrollListener(onLoadeMoreListener);
        api.getYouhui(1, 10,1);
        refresh.setOnRefreshListener(this);
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null && refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }

    }

    @Override
    public void onRequestStart() {
        if (refresh != null && !refresh.isRefreshing()) {
            refresh.setRefreshing(true);
        }
    }

    @Override
    public void getYouhuiSuccess(int pageIndex, BonusModel model) {
//        if (pageIndex <= model.pager.getAllPages()) {
        adapter.add(model.list, pageIndex == 1, model.borrow_amount);
//        } else {
//            ToastUtil.makeText(getApplicationContext(), getResources().getString(R.string.no_more_date_text), Toast.LENGTH_SHORT).show();
//        }
        if (adapter.getmSource().size() == 0 && model.list.size() <= 0) {
            nothing.setVisibility(View.VISIBLE);
        } else {
            nothing.setVisibility(View.GONE);
        }
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            if (loading != null) loading.setVisibility(View.VISIBLE);
        } else {
            if (loading != null) loading.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        onLoadeMoreListener.setPage(1, 0, false);
        api.getYouhui(1, 10,1);
    }


    @Override
    public void onClick(String bonus_id, String name, String money) {
        this.bonus_id = bonus_id;
        this.name = name;
        this.money = money;
        EventBus.getDefault().post(new YouhuiSelectEvent(bonus_id, name, money));
        finish();
    }

    @OnClick({R.id.layout_unuse})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {

            case R.id.layout_unuse:
                this.bonus_id = "";
                this.name = "";
                this.money = "0";
                EventBus.getDefault().post(new YouhuiSelectEvent(bonus_id, name, money));
                finish();
                break;
        }

    }
}
